package com.jni;

import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class NativeMethods {

    public enum SERIAL_FLASH_TYPE {
        SFT_UNKNOW(0),
        SFT_MXIC(1),
        SFT_ST(2),
        SFT_SST(3),
        SFT_ATMEL_AT25F(4),
        SFT_ATMEL_AT25FS(5),
        SFT_ATMEL_AT45DB(6),
        SFT_WINBOND(7),
        SFT_PMC(8),
        SFT_MXIC_LIKE(9),
        SFT_AMIC(10),
        SFT_EON(11),
        SF_ESMT(12),
        SFT_GIGA(13),
        SFT_FENTECH(14),
        SFT_UC(15),             //SFT_UCUNDATA
        SFT_BY(16),
        SFT_FM(17),
        SFT_ZB(18);

        private int value;
        private String name;

        private SERIAL_FLASH_TYPE(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public String getName() {
            return name;
        }

        public void setValue(int value){
            this.value = value;
        }
    }

    public enum ASIC_ROM_TYPE {
        ART_Unknow(-1),
        ART_32K(0),
        ART_64K(1),
        ART_128K(2),
        ART_256K(3);

        private int value;

        private ASIC_ROM_TYPE(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        private void setValue(int value){
            this.value = value;
        }
    }

    public class ChangeParamInfo
    {
        public int SerialNumberLength;      //SerialNumberLength = 6
        public int ProductLength;
        public int ManufactureLength;
        public int VidPidLength;            //VidPidLength = 8
        public int InterfaceLength;
        public int String3Length;

        public String strSerialNumber;      //Sample, strSerialNumber = "SN0001"
        public String strProduct;
        public String strManufacture;
        public String strVidPid;            //Sample, strVidPid="0c356362"
        public String strString3;
        public String strInterface;
    }

    static {
        //NativeMethods path = /com/jni/NativeMethods
        System.loadLibrary("SonixCamera");
    }


    private static NativeMethods instance;
    private static Lock mLock = new ReentrantLock();

    public static NativeMethods getInstance() {
        if (instance == null) {
            mLock.lock();
            if (instance == null) {
                instance = new NativeMethods();
            }
            mLock.unlock();
        }
        return instance;
    }

    private Object mCtrlTransferClass;

    public boolean Initialize(Object ctrlTransferClass) {
        this.mCtrlTransferClass = ctrlTransferClass;
        return Native_Initialize(ctrlTransferClass);
    }
    public native boolean Native_Initialize(Object ctrlTransferClass);


    public boolean UnInitialize() {
        return Native_UnInitialize();
    }
    public native boolean Native_UnInitialize();


    public boolean RestartDevice() {
        return Native_RestartDevice(mCtrlTransferClass);
    }
    public native boolean Native_RestartDevice(Object ctrlTransferClass);


    public boolean AsicRegisterRead(int addr, byte pData[], int length) {
        return Native_AsicRegisterRead(mCtrlTransferClass, addr, pData, length);
    }
    public native boolean Native_AsicRegisterRead(Object ctrlTransferClass, int addr, byte pData[], int length);


    public boolean AsicRegisterWrite(int addr, byte pData[], int length) {
        return Native_AsicRegisterWrite(mCtrlTransferClass, addr, pData, length);
    }
    public native boolean Native_AsicRegisterWrite(Object ctrlTransferClass, int addr, byte pData[], int length);


    public boolean SensorRegisterCustomRead(byte slaveId, int addr, byte dataByteNumber, byte pData[], byte dataByteBumber, boolean pollSCL) {
        return Native_SensorRegisterCustomRead(mCtrlTransferClass, slaveId, addr, dataByteNumber, pData, dataByteBumber, pollSCL);
    }
    public native boolean Native_SensorRegisterCustomRead(Object ctrlTransferClass, byte slaveId, int addr, byte dataByteNumber, byte pData[], byte dataByteBumber, boolean pollSCL);


    public boolean SensorRegisterCustomWrite(byte slaveId, int addr, byte dataByteNumber, byte pData[], byte dataByteBumber, boolean pollSCL) {
        return Native_SensorRegisterCustomWrite(mCtrlTransferClass, slaveId, addr, dataByteNumber, pData, dataByteBumber, pollSCL);
    }
    public native boolean Native_SensorRegisterCustomWrite(Object ctrlTransferClass, byte slaveId, int  addr, byte dataByteNumber, byte pData[], byte dataByteBumber, boolean pollSCL);

    public boolean SensorTwoRegisterCustomRead(byte slaveId, int addr, byte dataByteNumber, byte pData[], byte dataByteBumber, boolean pollSCL) {
        return Native_SensorTwoRegisterCustomRead(mCtrlTransferClass, slaveId, addr, dataByteNumber, pData, dataByteBumber, pollSCL);
    }
    public native boolean Native_SensorTwoRegisterCustomRead(Object ctrlTransferClass, byte slaveId, int addr, byte dataByteNumber, byte pData[], byte dataByteBumber, boolean pollSCL);


    public boolean SensorTwoRegisterCustomWrite(byte slaveId, int addr, byte dataByteNumber, byte pData[], byte dataByteBumber, boolean pollSCL) {
        return Native_SensorTwoRegisterCustomWrite(mCtrlTransferClass, slaveId, addr, dataByteNumber, pData, dataByteBumber, pollSCL);
    }
    public native boolean Native_SensorTwoRegisterCustomWrite(Object ctrlTransferClass, byte slaveId, int  addr, byte dataByteNumber, byte pData[], byte dataByteBumber, boolean pollSCL);


    public boolean XuRead(byte pData[], int length, byte cs, byte unitId) {
        return Native_XuRead(mCtrlTransferClass, pData, length, cs, unitId);
    }
    public native boolean Native_XuRead(Object ctrlTransferClass, byte pData[], int length, byte cs, byte unitId);


    public boolean XuWrite(byte pData[], int length, byte cs, byte unitId) {
        return Native_XuWrite(mCtrlTransferClass, pData, length, cs, unitId);
    }
    public native boolean Native_XuWrite(Object ctrlTransferClass, byte pData[], int length, byte cs, byte unitId);


    public boolean GetProduct(byte pData[], int length) {
        return Native_GetProduct(mCtrlTransferClass, pData, length);
    }
    public native boolean Native_GetProduct(Object ctrlTransferClass, byte pData[], int length);


    public boolean GetFwVersion(byte pData[], int length, boolean bNormalExport) {
        return Native_GetFwVersion(mCtrlTransferClass, pData, length, bNormalExport);
    }
    public native boolean Native_GetFwVersion(Object ctrlTransferClass, byte pData[], int length, boolean bNormalExport);


    public boolean GetFwVersionFromFile(String fw, byte pData[], int length, boolean bNormalExport) {
        return Native_GetFwVersionFromFile(mCtrlTransferClass, fw, pData, length, bNormalExport);
    }
    public native boolean Native_GetFwVersionFromFile(Object ctrlTransferClass, String fw, byte pData[], int length, boolean bNormalExport);


    public boolean SerialFlashRead(int addr, byte pData[], int length) {
        return Native_SerialFlashRead(mCtrlTransferClass, addr, pData, length);
    }
    public native boolean Native_SerialFlashRead(Object ctrlTransferClass, int addr, byte pData[], int length);


    public boolean SerialFlashSectorWrite(Object sft, int addr, byte pData[], int length) {
        return Native_SerialFlashSectorWrite(mCtrlTransferClass, sft, addr, pData, length);
    }
    public native boolean Native_SerialFlashSectorWrite(Object ctrlTransferClass, Object sft, int addr, byte pData[], int length);


    public boolean GetSerialFlashType(Object sft, boolean check) {
        return Native_GetSerialFlashType(mCtrlTransferClass, sft, check);
    }
    public native boolean Native_GetSerialFlashType(Object ctrlTransferClass, Object sft, boolean check);


    public boolean SerialFlashWrite(int addr, byte pData[], int length) {
        return Native_SerialFlashWrite(mCtrlTransferClass, addr, pData, length);
    }
    public native boolean Native_SerialFlashWrite(Object ctrlTransferClass, int addr, byte pData[], int length);


    public boolean GetVidPid(byte pData[], int length) {
        return Native_GetVidPid(mCtrlTransferClass, pData, length);
    }
    public native boolean Native_GetVidPid(Object ctrlTransferClass, byte pData[], int length);


    public boolean GetManufacturer(byte pData[], int length) {
        return Native_GetManufacturer(mCtrlTransferClass, pData, length);
    }
    public native boolean Native_GetManufacturer(Object ctrlTransferClass, byte pData[], int length);


    public boolean GetString3(byte pData[], int length) {
        return Native_GetString3(mCtrlTransferClass, pData, length);
    }
    public native boolean Native_GetString3(Object ctrlTransferClass, byte pData[], int length);


    public boolean GetInterface(byte pData[], int length) {
        return Native_GetInterface(mCtrlTransferClass, pData, length);
    }
    public native boolean Native_GetInterface(Object ctrlTransferClass, byte pData[], int length);


    public boolean GetSerialNumber(byte pData[], int length) {
        return Native_GetSerialNumber(mCtrlTransferClass, pData, length);
    }
    public native boolean Native_GetSerialNumber(Object ctrlTransferClass, byte pData[], int length);


    public boolean GetAsicRomType(Object romType, int chipID[]) {
        return Native_GetAsicRomType(mCtrlTransferClass, romType, chipID);
    }
    public native boolean Native_GetAsicRomType(Object ctrlTransferClass, Object romType, int chipID[]);

    public boolean WriteFwToFlash(Object onProgressClass, String fw, boolean bFullCheckFW) {
        return Native_WriteFwToFlash(mCtrlTransferClass, onProgressClass, fw, bFullCheckFW);
    }
    public native boolean Native_WriteFwToFlash(Object ctrlTransferClass, Object onProgressClass, String fw, boolean bFullCheckFW);


    public boolean DisableSerialFlashWriteProtect(Object sft) {
        return Native_DisableSerialFlashWriteProtect(mCtrlTransferClass, sft);
    }
    public native boolean Native_DisableSerialFlashWriteProtect(Object ctrlTransferClass, Object sft);


    public boolean EraseSerialFlash(Object sft) {
        return Native_EraseSerialFlash(mCtrlTransferClass, sft);
    }
    public native boolean Native_EraseSerialFlash(Object ctrlTransferClass, Object sft);


    public boolean EraseSectorFlash(Object sft, int addr) {
        return Native_EraseSectorFlash(mCtrlTransferClass, sft, addr);
    }
    public native boolean Native_EraseSectorFlash(Object ctrlTransferClass, Object sft, int addr);


    public boolean EraseBlockFlash(Object sft, int addr) {
        return Native_EraseBlockFlash(mCtrlTransferClass, sft, addr);
    }
    public native boolean Native_EraseBlockFlash(Object ctrlTransferClass, Object sft, int addr);


    public boolean BurnerFW(Object onProgressClass, String fw, Object sft, boolean bFullCheckFW) {
        return Native_BurnerFW(mCtrlTransferClass, onProgressClass, fw, sft, bFullCheckFW);
    }
    public native boolean Native_BurnerFW(Object ctrlTransferClass, Object onProgressClass, String fw, Object sft, boolean bFullCheckFW);


    public boolean CustomBurnerFW(Object paramInfo, Object onProgressClass, String fw, Object sft, boolean bFullCheckFW) {
        return Native_CustomBurnerFW(mCtrlTransferClass, paramInfo, onProgressClass, fw, sft, bFullCheckFW);
    }
    public native boolean Native_CustomBurnerFW(Object ctrlTransferClass, Object paramInfo, Object onProgressClass, String fw, Object sft, boolean bFullCheckFW);


    public boolean SetParamTableFromFWFile(Object paramInfo, Object onProgressClass, String fw, Object sft, boolean bFullCheckFW) {
        return Native_SetParamTableFromFWFile(mCtrlTransferClass, paramInfo, onProgressClass, fw, sft, bFullCheckFW);
    }
    public native boolean Native_SetParamTableFromFWFile(Object ctrlTransferClass, Object paramInfo, Object onProgressClass, String fw, Object sft, boolean bFullCheckFW);
}
